package com.datastructures.linkedList;

import java.util.Scanner;
import java.util.Stack;

/**
 * 单链表，每次添加到链表尾部
 *
 * @author MaoLin Wang
 * @date 2019/10/1819:14
 */
public class SingleLinkedListDemo {
    public static void main(String[] args) {

        //先创建几个节点
      /*  Person person1 = new Person(1, "datastructures", "小m");
        Person person2 = new Person(2, "wml2", "小m2");
        Person person3 = new Person(3, "wml3", "小m3");
        Person person4 = new Person(4, "wml4", "小m4");
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.add2(person1);
        singleLinkedList.add2(person4);
        singleLinkedList.add2(person2);
        singleLinkedList.add2(person3);
        singleLinkedList.printData();
        System.out.println("-----------");
        SingleLinkedList.reverseList(singleLinkedList.getHead());
        singleLinkedList.printData();
        System.out.println("----------倒叙打印");
        SingleLinkedList.reversePrint(singleLinkedList.getHead());*/

        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入链表A的长度");
        int Alength=scanner.nextInt();
        System.out.println("请输入链表B的长度");
        int Blength=scanner.nextInt();
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        Person personA=null;
        for(int i=0;i<Alength;i++){
            for (int j=0;j<Blength;j++){
                if (j == Blength-1){
                     personA=new Person("x"+(i+1),"","");
                    singleLinkedList.add(personA);

                }
                if( i ==j){
                     personA=new Person("x"+(i+1),"","");
                    Person personB=new Person("y"+(i+1),"","");

                    singleLinkedList.add(personA);
                    singleLinkedList.add(personB);

                }else if (j>i ||j>i){
                    break;
                }
            }

        }
        singleLinkedList.printData();
        Person person = SingleLinkedList.reverseDigui(singleLinkedList.getHead());

        System.out.println("------------------");

        singleLinkedList.printData(person);
     //   person2.nickname = "修改的昵称";
       // singleLinkedList.update(person2);
        //singleLinkedList.update(person2);
        //singleLinkedList.printData();
      //  singleLinkedList.delete(2);
      //  singleLinkedList.delete(4);
       // singleLinkedList.printData();
      //  System.out.println(SingleLinkedList.findLastINdexNode(singleLinkedList.getHead(),3));
      //  System.out.println(SingleLinkedList.getLength(singleLinkedList.getHead()));

    }
}

//定义单链表
class SingleLinkedList {
    //先初始化一个头节点，头结点不动
    private Person head = new Person("0", "", "");

    public Person getHead() {
        return this.head;
    }
    //添加节点到单链表

    /**
     * 不考虑编号顺序时
     * 1.找到当前链表的最后节点
     * 2.将最后这个节点
     */
    public void add(Person person) {
        //head节点不能动，因此需要一个辅助遍历temp
        Person temp = head;
        //遍历链表，找到最后
        while (true) {
            if (temp.next == null) {
                break;
            }
            //没找到，就将temp后移
            temp = temp.next;
        }
        //temp已指向最后，添加新的节点
        temp.next = person;
    }


    /**
     * 根据no将数据插入到指定位置
     */
    public void add2(Person person) {
        //temp是位于添加位置的前一个节点，否则插入不了
        Person temp = head;
        boolean flag = false;//标识添加的编号是否存在
        while (true) {
            if (temp.next == null) {
                //temp在链表最后
                break;
            }
            if (Integer.parseInt(temp.next.no) > Integer.parseInt(person.no)) {//位置找到，应该加载temp和temp.next中间
                break;
            } else if (temp.next.no .equals(person.no)) {
                //已经存在
                flag = true;
            }
            temp = temp.next;

        }
        if (flag) {
            System.out.println("已经存在，不可添加");
        }
        //添加节点
        person.next = temp.next;
        temp.next = person;
    }

    /**
     * 修改，根据no编号修改，即no不可改
     * 1.根据新的数据的no修改
     */
    public void update(Person newPerson) {

        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //找到需要修改的节点
        Person temp = head.next;
        boolean flag = false;//是否找到该节点
        while (true) {
            if (temp == null) {
                break;//遍历完链表
            }
            if (temp.no .equals( newPerson.no)) {
                //找到了
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (!flag) {
            System.out.println("未找到该节点");
        }
        temp.name = newPerson.name;
        temp.nickname = newPerson.nickname;
    }

    /**
     * 删除节点
     *
     * @param no
     */
    public void delete(String no) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        Person temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no.equals(no)) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.next = temp.next.next;

        } else {
            System.out.println("节点不存在:" + no);
        }
    }

    /**
     * 获取单链表有效个数
     *
     * @param head 头结点
     * @return 返回有效节点个数
     */
    public static int getLength(Person head) {
        if (head.next == null) {
            return 0;
        }
        int length = 0;
        Person p = head.next;
        while (p != null) {
            length++;
            p = p.next;
        }
        return length;
    }

    /**
     * 查找单链表中的倒数第k个节点(新浪)
     * 1.编写一个方法，接收head节点，同时接收一个index
     * 2.index表示倒数第index个节点
     * 3.先把链表整体遍历，得到总长度
     * 4.从队列链表第一个开始遍历(siz-index)个
     */
    public static Person findLastINdexNode(Person head,int index){
        if(head.next==null){
            return null;
        }
        int length=getLength(head);
        if(length<=0||index<=0||length>length){
            System.out.println("不合法的索引");
            return null;
        }
        Person temp=head.next;
        for (int i=0;i<length-index;i++){
            temp=temp.next;
        }
        return temp;

    }

    public static Person reverseDigui(Person head){
        if (head.next==null){
            return null;
        }
        if (head.next.next==null){
            return head.next;
        }
        Person temp=head.next.next;
        Person rever=reverseDigui(temp);
        temp.next=head;
        head.next=null;
        return temp;

    }

    /**
     * 链表反转
     * @param head
     */
    public static void reverseList(Person head){
        if(head.next==null||head.next.next==null){
            return ;
        }
        Person newList=new Person("0","","");
        /**
         * 用于遍历原链表
         */
        Person temp=head.next;
        Person next=null;//指向temp的下一个节点
        //遍历原理的链表，每遍历一个节点就将其取出放入newList
        while (temp!=null){
            next=temp.next;
            temp.next=newList.next;//将temp的下一个节点指向新的链表的最前端
            newList.next=temp;//temp连接到新链表上
            temp=next;//temp后移
        }

        head.next= newList.next;//旧头节点拼接在新链表上
        newList.next=null;
    }

    /**
     * 倒叙打印
     */
    public static void reversePrint(Person head){
        if(head.next==null){
            return;
        }
        Stack<Person>stack=new Stack<>();
        Person temp=head;
        while (temp.next!=null){
            stack.push(temp.next);
            temp=temp.next;
        }
       while (stack.size()>0){
           System.out.println(stack.pop());
       }
    }

    public void printData() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        Person temp = head.next;
        while (true) {
            //是否到最后了
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }
    public void printData(Person head) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        Person temp = head.next;
        while (true) {
            //是否到最后了
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }
}

class Person {
    public String no;
    public String name;
    public String nickname;
    public Person next;//指向下一个节点

    public Person(String no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "Person{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +

                '}';
    }
}
